<!-- Web Upload Data Result PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 输入的 P/N, S/N 转大写
		3. Upload 的 FTP 增加文件视窗选择, 是否上传档案勾选判断
		4. 上传 FTP 文件
		5. 上传文件不能为中文判断
		6. Upload 的 WSID 增加重复多笔站建立
	Rev 1.01a at 2015/07/17 by Max Yin:
		1. 增加 check is_dir(), 若 ftp 上 ftp 没有建立 ftp/文件夹时, 自动建立(TA-Weibaio)
	Rev 1.02 at 2015/07/28
		1. 修正, update ftp ip 只有文件夹, 没有文件的问题。
	Rev 1.04 at 2015/09/09 by Max Yin:
		1. 新增 2D Label 的 S/N 字串范围的判斷。
	Rev 1.05 at 2015/11/22 by Max Yin:
		1. 新增加 SMT 部分的a.线别Line,b.板面Panel。
	Rev 1.07 at 2015/11/30 by Max Yin,
		1. 解决 Web 上传系统, 若有重复的 P/N, 站别, 版本名没有拦截下来, 导致重复建立站别。(PPE-Shaoxin_huang@JOYTECH)
		2. 解决 Web 下载系统, select name='webul_select_wsid', 设定错误写成 select id='webul_select_wsid'的问题。(SMT-eric_liang@joytech )
	Rev 1.08 at 2015/12/08 by Max Yin,
		1. Web SMT upload 增加-回流焊的选项。(eric_liang)
		2. Web SMT upload/download, 由填入栏位改为下拉选单。(eric_liang)
		3. Web Upload/Downlaod 新增加 ESD 下拉选单。(jianwei_wang)
		4. 移除 ftp IP 的栏位输入, 由 Web 程式内定 FTP 路径。(Lucas)
		5. 修正重复上传, 却只有版本不对, 导致无法上传问题。(mingshen_feng)
		6. 修正若上传者, 若没有上传者名称, 会要求重新登入讯息。
	Rev 1.28 at 2016/11/17 by weibiao_yuan	,
		1.增加上传文件名及文件类型的卡关机制。
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
		<script type="text/javascript">
			/**
			*返回浏览器类型(ie,firefox,opera)
			*/
			function getBrowser(){
				var type = "";
				var Browser = {
				  'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
				  'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
				  'isOpera' : navigator.userAgent.indexOf('Opera') >= 0
				};
				if(Browser["isIE"]==true){type='ie'};
				if(Browser["isFirefox"]==true){type='firefox'};
				if(Browser["isOpera"]==true){type='opera'};
				return type;
			}

			/**
			*生成静态进度条
			*@param progress	进度(例如：10/120,也可为文字:进度不正常时的提示文字)
			*@param id		进度条id 大家注意生成进度条时传入id，这个id就给了大家丰富的想象空间，有个这个id就可以干很多事哦~
			*@param color 进度条颜色
			*/
			function processBar(progress,id,color){
				var browser = getBrowser();
				var height = "";
				if(browser.toLowerCase()=='ie'){
					height = 28;
				}else if(browser.toLowerCase()=='firefox'){
					height = 24;
				}else if(browser.toLowerCase()=='opera'){
					height = 24;
				}
				
				var processStr = "";
				if(parseInt(progress)<90){//进度显示放在span外
					processStr = 
					// CSS formate ...
					'<div id="'+id+'" style="height:24px;text-align:left;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';width:'+progress+'%;"></div>'+progress+"%"+
					'</div>';
				} else if(parseInt(progress)>=90) {//进度显示放在span中
					processStr = 
					'<div id="'+id+'" style="height:24px;text-align:left;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';width:'+progress+'%;">'+progress+"%"+'</div>'+
					'</div>';
				}else{
					processStr = 
					'<div id="'+id+'" style="height:24px;text-align:center;background:white;border: 1px solid green;cursor:hand;" >'+
						'<div style="float:left;height:'+height+'px;text-align:center;background:'+color+';"></div>'+progress+
					'</div>';
				}
				return processStr;
			}
		</script>
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 程式/文件,上传中 ---</b></font><br>
			<hr>
			<?php
				require_once('class_package/common.php');
				// $tmp_pn = CRL615766000A,CRL615766007A,...
				$tmp_pn				= strtoupper($_POST['webul_pn']);
				$tmp_wsid     	 	= strtoupper($_POST['webul_wsid']); 
				$tmp_work_order_number     	 	= trim(strtoupper($_POST['webul_work_order_number'])); //为工单号创立
				$tmp_tpver    		= trim(strtoupper($_POST['webul_mfg_ver']));
				$tmp_work_order		= strtoupper($_POST['webul_work_order']);
				$tmp_line			= strtoupper($_POST['webul_line']);
				$tmp_panel			= strtoupper($_POST['webul_panel']);
				$tmp_webul_label		= strtoupper($_POST['webul_label']);
				$upload_tmpname  	= $_FILES['webul_upload_file']['tmp_name'];
				$upload_filename 	= $_FILES['webul_upload_file']['name'];
				$tmp_descript 		= $_POST['webul_descript'];
				$tmp_by				= $_COOKIE['login_user'];
				// $result1 			= preg_match("/^ /",$tmp_tpver);
				// $result2 			= preg_match("/ $/",$tmp_tpver);
				$ar					= array('rar','zip','7z','exe','docx','doc','xlsx');//若须新增，只需将后缀添至数组即可
				// $at					= substr($upload_filename,-4);
				$at					= pathinfo("$upload_filename",PATHINFO_EXTENSION);
				$arra				= explode('.',$upload_filename);
				$arr_ex				= explode(' ',$upload_filename);
				if(count($arr_ex) > 1){
					$upload_filename = implode('',$arr_ex);
				}
				if ( $tmp_by == '' ) {
					echo "<script>alert('登入帐号不能为空值, 请回到 http://webdl.joytech.com.cn/webul_login.html 使用者登入网页, 重新登入!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_wsid) == 0 || strlen($tmp_wsid) > 60 ) {
					echo "<script>alert('输入的-WSID(站別)或其他栏位中, 不能为空值或超过60个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_tpver) == 0 || strlen($tmp_tpver) > 30 ) {
					echo "<script>alert('输入的-TPVER(程式版本)或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_tpver,' ') > -1 ) {
					echo "<script>alert('输入的TPVER(程式版本)的栏位中, 不能为/' /'空白,请用/'_/'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_webul_label,' ') > -1) {
					echo "<script>alert('输入的label(烧录帖子样板)的栏位中, 不能为/' /'空白,请用/'_/'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_webul_label) > 30) {
					echo "<script>alert('输入的label(烧录帖子样板)的栏位中, 或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				// if($result1 || $result2){
					// echo "<script>alert('输入的-TPVER(程式版本)栏位中, 版本名称前和版本名称后不能有空格, 请重新输入!!');history.go(-1);</script>";
					// exit();
				// }
				if ( strlen($tmp_line) == 0 || strlen($tmp_line) > 30 ) {
					echo "<script>alert('输入的-线别Line或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_panel) == 0 || strlen($tmp_panel) > 30 ) {
					echo "<script>alert('输入的-板面Panel或其他栏位中, 不能为空值或超过30个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($upload_filename) == 0 ) {
					echo "<script>alert('输入的-Upload File(上传文件)或其他栏位中, 不能为空值, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if (strpos($upload_filename,'+') > -1 ) {
					echo "<script>alert('输入的Upload File(上传文件)的栏位中, 不能为\' \'空白或\'+\'字符,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strlen($tmp_descript) == 0 || strlen($tmp_descript) > 255 ) {
					echo "<script>alert('输入的-Description(描述)或其他栏位中, 不能为空值或超过255个字符, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_wsid,'SMT') > -1  ) {
					if ( $tmp_line == 'N/A' ) {
						echo "<script>alert('输入的栏位中,线别Line,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
					
					if ( $tmp_panel == 'N/A' ) {
						echo "<script>alert('输入的栏位中,板面Panel,不能为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				if ( strpos($tmp_wsid,'SMT') > -1 ||strpos($tmp_wsid,'REPAIR') > -1 || strpos($tmp_wsid,'ICT') > -1 || strpos($tmp_wsid,'SCT') > -1 || strpos($tmp_wsid,'H/P') > -1 || strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'TOOL') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
					if ( $tmp_work_order != 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				} else {
					if ( $tmp_work_order == 'N/A' ) {
						echo "<script>alert('输入的-Work Order(工單)不应为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
						exit();
					}
				}
				if($tmp_pn==$arra[0]){
					echo "<script>alert('上传的文件名和part number不能一致，请重新修改上传!!');history.go(-1);</script>";
					exit();
				}
				if(!(in_array("$at",$ar))){					
					echo "<script>alert('上传的文件格式须为.rar, .zip, .7z, .exe,.doc, .docx,.xlsx,请重新上传!!');history.go(-1);</script>";
					exit();
				}
				if ( $tmp_wsid == "FLASH" && strpos($tmp_descript,'/') == NULL ) {
					echo "<script>alert('上传的FLASH的Description(描述)必须为/'Location/ChkSum/',Ex:/'U12/BAD7/'的格式,请重新上传!!');history.go(-1);</script>";
					exit();
				}
				if ( $tmp_wsid == "FLASH" && empty($tmp_work_order_number)&& $tmp_work_order != "G") {
					echo "<script>alert('上传的FLASH的工单号不能为空,请重新上传!!');history.go(-1);</script>";
					exit();
				}
				
				// if( $tmp_wsid == "FLASH" ){
					// $db_select      = Sql_Download::sql_db;
					// $db_table       = Sql_Download::sql_table;
					// $table_name1    = Sql_Download::table_name1;	// 'webdl_pn';
					// $table_name2	= Sql_Download::table_name2;	// 'webdl_create_date';
					// $table_name4	= Sql_Download::table_name4;	// 'webdl_creator';																																					
					// $conn			= '';
					// $sql_cmd		= '';
					// $result			= '';
					
					
					// $conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
					// if ( mysqli_connect_errno($conn) ) { 
						// echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
						// exit();
					// }
					// mysqli_set_charset($conn,'set names utf8');											
					// $sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$tmp_pn' AND `$table_name2`='$tmp_wsid' AND `$table_name4`='$tmp_work_order'";
					// $result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
					// if ( $result ) { 
						// $rows = mysqli_num_rows($result);	// 返回结果集中栏位的数量。
						// if ( $rows != NULL ) { 
							// mysqli_close($conn);			// 关闭资料库
							// echo "<script>alert('请先删除该料号下对应的旧资料');history.go(-1);</script>";
							// exit();
						// }
					// }
					// mysqli_close($conn);					// 关闭资料库
				// }				
				
				if( $tmp_wsid == "FLASH" ){
					$db_select      = Sql_Download::sql_db;
					$db_table       = Sql_Download::sql_table;
					$table_name1    = Sql_Download::table_name1;	// 'webdl_pn';
					$table_name2	= Sql_Download::table_name2;	// 'webdl_create_date';
					$table_name4	= Sql_Download::table_name4;	// 'webdl_creator';																																					
					$conn			= '';
					$sql_cmd		= '';
					$result			= '';
					
					$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
					if ( mysqli_connect_errno($conn) ) { 
						echo "<script>alert('MySQL资料库连接错误, 请联系统开发人员!!');history.go(-1);</script>";
						exit();
					}
					
					mysqli_set_charset($conn,'set names utf8');
					$sqli_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$tmp_pn' AND `$table_name2`='$tmp_wsid' AND `$table_name4`='$tmp_work_order'";
					$res = mysqli_query($conn,$sqli_cmd);	// 透过 execute_query 对象实例完成查询
					$row = mysqli_num_rows($res);		// 返回结果集中栏位的数量。
					if ( $row > 0 ) {
						 // echo "<script>alert('请先删除该料号下对应的旧资料');history.go(-1);</script>";
						while ( $getArrays = mysqli_fetch_array($res,MYSQLI_ASSOC) ) {					
							$put_pn          = $getArrays['webdl_pn'];
							$put_wsid        = $getArrays['webdl_wsid'];
							$put_tpver       = $getArrays['webdl_tpver'];
							$put_work_order  = $getArrays['webdl_work_order'];
							$put_line        = $getArrays['webdl_line'];
							$put_panel       = $getArrays['webdl_panel'];
							$put_create_date = $getArrays['webdl_create_date'];
							$put_creator     = $getArrays['webdl_creator'];
							$put_description = $getArrays['webdl_description'];
							$put_ftpip       = $getArrays['webdl_ftpip'];
														
							@unlink($put_ftpip);			
							download_delete($put_pn,$put_wsid,$put_tpver,$put_work_order,$put_line,$put_panel);

						}
					}
					mysqli_close($conn);	// 关闭资料库
				}
				
				// To create new data columns...
				if ( strpos($tmp_pn,',') > -1 )
					$get_pn_array = preg_split("/[\s,]+/",$tmp_pn);
				else
					$get_pn_array[] = $tmp_pn;
				
				$PN_chkcnt		= 0;
				$getPN_ArrCnt	= count($get_pn_array);
				foreach ( $get_pn_array as $each_pn ) {
					$each_ftpip    	= GetFtpPath($tmp_wsid,$tmp_tpver,$each_pn,$tmp_line,$tmp_work_order,$tmp_panel);
					if ( substr($each_ftpip,-1) != "/" ) $each_ftpip = sprintf("%s%s",$each_ftpip,"/");
					$each_ftpipfile	= sprintf("%s%s",$each_ftpip,$upload_filename);
					// echo "each_pn				= $each_pn<br>";
					// echo "tmp_wsid				= $tmp_wsid<br>";
					// echo "tmp_tpver			= $tmp_tpver<br>";
					// echo "tmp_work_order	= $tmp_work_order<br>";
					// echo "tmp_line			= $tmp_line<br>";
					// echo "tmp_panel			= $tmp_panel<br>";
					// echo "each_ftpip			= $each_ftpip<br>";
					// echo "upload_tmpname		= $upload_tmpname<br>";
					// echo "upload_filename		= $upload_filename<br>";
					// echo "each_ftpipfile		= $each_ftpipfile<br>"; exit();
					// echo "tmp_descript		= $tmp_descript<br>";
				
					// each_pn = CRL615766003A
					// tmp_wsid = SCT
					// tmp_tpver = 0X1.18
					// each_ftpip = ftp://ICT:!xiuqun123@10.10.30.65/ICT/[path]/
					// upload_tmpname = C:\xampp\tmp\php3712.tmp
					// upload_filename = ASC5766BBS-FLF-61P_004_Main_IMMx_Max.7z
					// each_ftpipfile = ftp://ICT:!xiuqun123@10.10.30.65/ICT/[path]/ASC5766BBS-FLF-61P_004_Main_IMMx_Max.7z
					// tmp_descript = ASC5766BBS-FLF-61P_10SFP_1XFP
				
					// To check user name & password limit ...
					if ( strlen($each_pn) == 0 || strlen($each_pn) > 13 ) {
						echo "<script>alert('输入的-Part Number(局部料号), 逗号\',\'后, 不能为空值或超过13个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($tmp_wsid,'ROUTER') > -1 || strpos($tmp_wsid,'TOOL') > -1 || strpos($tmp_wsid,'WS-PROFILE') > -1 || strpos($tmp_wsid,'DOC') > -1 || strpos($tmp_wsid,'ESD') > -1 ) {
						if ( $each_pn != 'N/A' ) {
							echo "<script>alert('输入的栏位中,Part Number(局部料号)不为\'N/A\'字串,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					} else {
						if ( strlen($each_pn) != 13 ) {
							echo "<script>alert('输入的查询栏位中-Part Number(局部料号),应该为 13 个字元,请重新输入!!');history.go(-1);</script>";
							exit();
						}
					}
					if ( strlen($each_ftpip) == 0 || strlen($each_ftpip) > 255 ) {
						echo "<script>alert('输入的-FTP Path(FTP上传路径)或其他栏位中, 不能为空值或超过255个字符, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					if ( strpos($each_ftpip,' ') > -1 ) {
						echo "<script>alert('输入的FTP Path(FTP上传路径)的栏位中, 不能为\' \'空白字符,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
						exit();
					}
					
					// To get each ftp ip inside of values ...
					// ftp://ICT:!xiuqun123@10.10.30.65/ICT/[path]/
					$get_ftpip_array  = preg_split("/[\<\>\:\/\@]+/",$each_ftpip);
					// var_dump ( $get_ftpip_array );
					// foreach ( $get_ftpip_array as $key => $str ) echo "FTP IP Key = $key, String = $str<br>";
					// each_ftpip = ftp://rdta:rdta@10.10.30.65/Max_Yin/test/
					// FTP IP Key = 0, String = ftp
					// FTP IP Key = 1, String = rdta
					// FTP IP Key = 2, String = rdta
					// FTP IP Key = 3, String = 10.10.30.65
					// FTP IP Key = 4, String = Max_Yin
					// FTP IP Key = 5, String = test
					// FTP IP Key = 6, String = 
					$getArrCnt    = count($get_ftpip_array);
					// echo "getArrCnt = $getArrCnt<br>";
					// getArrCnt = 7
				
					$ftp_host     = $get_ftpip_array[3];
					// echo "ftp_host = $ftp_host<br>";
					// ftp_host = 10.10.30.65
					
					$ftp_username = $get_ftpip_array[1];
					$ftp_password = $get_ftpip_array[2];
					// echo "ftp_username = $ftp_username<br>";
					// echo "ftp_password = $ftp_password<br>";
					// ftp_username = ICT
					// ftp_password = !xiuqun123
					
					// each_ftpip = ftp://ICT:!xiuqun123@10.10.30.65/ICT/[path]/
					$start = strpos($each_ftpip,$get_ftpip_array[3])+strlen($get_ftpip_array[3]);	//检索字符串中首次出现的位置。
					// $getEndStr    = $get_ftpip_array[$getArrCnt-2];
					$EndStrLen    = strlen($get_ftpip_array[$getArrCnt-2]);
					$getEndStr    = strripos($each_ftpip,$get_ftpip_array[$getArrCnt-2]);
					$getEndStr    += $EndStrLen;									//检索字符串中末尾出现的位置。
					$end          = $getEndStr;
					// echo "start = $start<br>";
					// echo "end = $end<br>";
					// start = 32
					// end = 37
					
					$ftp_destTmp        = substr($each_ftpip,$start,$end-$start);
					$ftp_destDir        = sprintf("%s/",$ftp_destTmp);
					// echo "ftp_destTmp = $ftp_destTmp<br>";
					// echo "ftp_destDir = $ftp_destDir<br>";
					// ftp_destTmp = /ICT/
					// ftp_destDir = /ICT/[path]/
					
					// 判断上传文件不能为中文, 而关 gbk是双字节，utf8是三字节，可以根据中文的范围来判断 
					// UTF-8 (Unicode)   
					// \u4e00-\u9fa5 (中文)   
					// \x3130-\x318F (韩文   
					// \xAC00-\xD7A3 (韩文)   
					// \u0800-\u4e00 (日文)   
					// ps: 韩文是大于[\u9fa5]的字符   
					// 正则例子:   
					// preg_replace(”/([\x80-\xff])/”,”",$str);  
					// preg_replace(”/([u4e00-u9fa5])/”,”",$str);
					$get_file_name_array  = preg_split("/[\_\-\(\)\.\<\>]+/",$upload_filename);
					// foreach ( $get_file_name_array as $ArrKey => $each_name ) echo "$ArrKey => $each_name<br>";
					// AcctonTest_Automa(中文测试)tion_0V-20110629.rar
					// 0 => AcctonTest
					// 1 => Automa
					// 2 => 中文测试abc
					// 3 => tion
					// 4 => 0V
					// 5 => 20110629
					// 6 => rar
					$err_chk = 0;
					foreach ( $get_file_name_array as $each_name ) {
						if( preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$each_name) ) {
							$err_chk = 1;
							break;
						}
					}
					if ( $err_chk == 1 ) {
						echo "<script>alert('输入的FTP IP(FTP文件上传路径)栏位中,不能有中文(汉)字.');history.back(-1);</script>";die;
						exit();
					}
					$ftp_host = '192.168.191.1';
					$ftp_username = '';		// FTP Web update user name
					$ftp_password = '';
					// open connection & send access parameters ...
					$conn         = ftp_connect($ftp_host);
					$login_result = ftp_login($conn, $ftp_username,$ftp_password);
					// var_dump ( $conn );
					// var_dump ( $login_result );
					
					if ( (!$conn) || (!$login_result) ) { 
						echo "<script>alert('FTP 登入失败, 请确认登入IP,帐号及密码是否正确后, 再试一次!!');history.go(-1);</script>";
						exit();
					}
					ftp_pasv($conn,true); // 打开被动模式传输 (PASV MODE)，否则则关闭被动传输模式。 
					
					// delete ftp old file ...
					if ( is_file($each_ftpipfile) ) {
						unlink($each_ftpipfile);
					} elseif ( !is_dir($each_ftpip) ) {
						// if not found ftp path then reate it.
						if ( !mkdir($each_ftpip, 0777, true) ) {
							ftp_close($conn);
							echo "<script>alert('建立 FTP 上传的文件夹失败, 请再重新操作一次!!');history.go(-1);</script>";
							exit();
						}
					}
					
					// perform. file upload then to check upload status, display message ...
					echo "<br>";
					// filesize($file)
					// 开始
					// echo "conn = ";
					// var_dump($conn);
					// echo "<br>";
					// echo "ftp_destDir & filename = ";
					// var_dump($ftp_destDir.$upload_filename);
					// echo "<br>";
					// echo "upload_tmpname = ";
					// var_dump($upload_tmpname);
					// echo "<br>";
					// $PN_chkcnt++;
					// if ( $PN_chkcnt == 1 ) continue; else exit();
					
					if (0) {
						$upload = ftp_put($conn,$ftp_destDir.$upload_filename,$upload_tmpname,FTP_BINARY);
						if ( !$upload ) {
							ftp_close($conn);
							echo "<script>alert('上传 FTP 文件失败, 请再重新操作一次!!');history.go(-1);</script>";
							exit();
						}
					} else {
						// echo "conn = $conn<br>";
						// echo "ftp_destDir.upload_filename = $ftp_destDir.$upload_filename<br>";
						// echo "upload_tmpname = $upload_tmpname<br>";
						// exit();
						$ret = ftp_nb_put($conn,$ftp_destDir.$upload_filename,$upload_tmpname,FTP_BINARY);		
						$iCnt = 1;
						while ( $ret == FTP_MOREDATA ) {
							// 加入其它要执行的代码
							if ( $iCnt%100 == 0 )
								echo ".<br>";
							else 
								echo ".";

							// 继续传送...
							$ret = ftp_nb_continue($conn);
							$iCnt++;
						}

						if ( $ret != FTP_FINISHED ) {
							ftp_close($conn);
							echo "<script>alert('FTP 上传文件中发生失败, 请再试一次!!');history.go(-1);</script>";
							exit();
						}
					}
					ftp_close($conn);
					
					// delete local copy of uploaded file
					$PN_chkcnt++;
					if ( $PN_chkcnt == $getPN_ArrCnt ) {
						if ( !unlink($upload_tmpname) ) {
							echo "<script>alert('暂存文件删除失败, 请再重新操作一次!!');history.go(-1);</script>";
							exit();
						}
					}
					
					//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
					// $keywords = preg_split("/[\s,]+/", "hypertext language, programming");
					// print_r($keywords);
					// Array
					// (
						// [0] => hypertext
						// [1] => language
						// [2] => programming
					// )
					// $tmp_wsid = P/T, F/T-B,F/T-B2
					if ( strpos($tmp_wsid,',') > -1 )
						$get_wsid_array = preg_split("/[\s,]+/",$tmp_wsid);
					else
						$get_wsid_array[] = $tmp_wsid;
					
					$db_select   = Sql_Download::sql_db;
					$db_table    = Sql_Download::sql_table;
					$table_name1 = Sql_Download::table_name1;	//webdl_pn
					$table_name2 = Sql_Download::table_name2;	//webdl_wsid
					$table_name3 = Sql_Download::table_name3;	//webdl_tpver
					$table_name4 = Sql_Download::table_name4;	//webdl_work_order
					$table_name5 = Sql_Download::table_name5;	//'webdl_line';
					$table_name6 = Sql_Download::table_name6;	//'webdl_panel';
					$Station_Obj = new Stations();
					$err_chk     = '';
					// echo "db_select		= $db_select<br>";
					// echo "db_table		= $db_table<br>";
					// echo "table_name1	= $table_name1<br>";
					// echo "table_name2	= $table_name2<br>";
					// echo "table_name3	= $table_name3<br>";
					// echo "table_name4	= $table_name4<br>";
					foreach ( $get_wsid_array as $each_wsid ) {
						// To search all table ...
						$conn		= '';
						$sql_cmd	= '';
						$result		= '';
						$getArray	= '';
						$get_pn		= '';
						$get_wsid	= '';
						$get_tpver	= '';
						
						// To check the WSID is exist ...
						$get_StationChk = $Station_Obj->Station_Exist($each_wsid);
						if ( $get_StationChk == 0 ) {
							$err_chk = 'Station_not_exist';
							break;
						}
						
						// Check connection
						$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
						if ( mysqli_connect_errno($conn) ) { 
							$err_chk = 'SQL_Conn_Fail';
							break;
						}
						mysqli_set_charset($conn,'set names utf8');
						
						// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
						// SELECT *  FROM `download_data` WHERE `pn` = 'CRL615766007A' AND `wsid` = 'F/T-B1' AND `tpver` = '0X1.16'
						$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1`='$each_pn' AND `$table_name2`='$each_wsid' AND `$table_name3`='$tmp_tpver' AND `$table_name4`='$tmp_work_order' AND `$table_name5`='$tmp_line' AND `$table_name6`='$tmp_panel'";
						$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
						if ( $result ) { 
							$rows = mysqli_num_rows($result);	// 返回结果集中栏位的数量。
							if ( $rows != NULL ) { 
								mysqli_close($conn);			// 关闭资料库
								$err_chk = 'WSID_Repear';
								break;
							}
						}
						mysqli_close($conn);					// 关闭资料库
						
						// To create the account user name and password to MySQL ...
						date_default_timezone_set("Asia/Taipei");	// 定义时区为 亚洲/台北
						$each_date = date('Y-m-d G:i:s');			// 定义显示时间格式 2015-06-14 14:24:30						
						
						$myfile = fopen("D:/webulfile.txt", "a") or die("Unable to open file!");						
						$c="\r\n$tmp_work_order $each_pn $each_wsid $tmp_tpver $each_date $tmp_by";
						fwrite($myfile, $c);
						fclose($myfile);//关闭该操作
						if ( $tmp_wsid == "FLASH" ) {
							//echo "<script>alert('$each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel,$each_ftpipfile,$each_date,$tmp_by,$tmp_descript,$tmp_work_order_number,$tmp_webul_label')</script>";
							$result =download_flashcreate($each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel,$each_ftpipfile,$each_date,$tmp_by,$tmp_descript,$tmp_work_order_number,$tmp_webul_label);
						}else {
							$result = download_create($each_pn,$each_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel,$each_ftpipfile,$each_date,$tmp_by,$tmp_descript);
						}
						if ( !$result ) {
							//echo "<script>alert('Creat_Fail')</script>";
							$err_chk = 'Creat_Fail';
							break;
						}
						if( !file_exists($each_ftpipfile)){
						
							$err_chk = 'Creat_Fail';
							break;
						}
					}
				
					if ( $err_chk == 'Station_not_exist' ) {
						echo "<script>alert('在 WSID(站別) 栏位中, 有不存在的站别, 请重新再输入一次!!');history.go(-1);</script>";
						exit();
					} elseif ( $err_chk == 'SQL_Conn_Fail' ) {
						echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
						exit();
					} elseif ( $err_chk == 'WSID_Repear' ) {
						echo "<script>alert('在上传的WSID(站別)栏位, 已经有资料库建立过了, 无须重复建立!');history.go(-1);</script>";
						exit();
					} elseif ( $err_chk == 'Creat_Fail' ) {
						echo "<script>alert('该上传资料, 建立失败! 请联系程式开发人员, 并查明原因...');history.go(-1);</script>";
						exit();
					}
					unset($get_wsid_array);
				}
				unset($get_pn_array);
				echo "<script>alert('该上传资料, 都已建立成功!');location.href='webul_data.html';</script>";
				exit();
			?>
		</center>
	</body>
</html>

